package com.lego.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lego.bean.Goods;
import com.lego.bean.Seller;
import com.lego.bean.SellerGoodsReviewVO;
import com.lego.bean.UserReviewVO;
import com.lego.service.GoodsService;
import com.lego.service.OrdersService;
import com.lego.service.SellerService;
import com.lego.service.UserService;
import com.lego.service.impl.GoodsServiceImpl;
import com.lego.service.impl.OrdersServiceImpl;
import com.lego.service.impl.SellerServiceImpl;
import com.lego.service.impl.UserServiceImpl;

@WebServlet("/purchase.do")
public class PurchaseServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GoodsService gds = new GoodsServiceImpl();
		OrdersService ods = new OrdersServiceImpl();
		SellerService ss = new SellerServiceImpl();
		UserService us = new UserServiceImpl();
		request.setCharacterEncoding("utf-8");
		int goodsId = 0;
		try {
			goodsId = Integer.parseInt(request.getParameter("goodsId"));
		} catch (Exception e) {
			System.out.println("没有查询到该商品信息");
		}
		//查询商品信息
		Goods goods = gds.selectByGoodsId(goodsId);
		System.out.println(goods);
		//查询商家相关信息
		int sellerId = goods.getSellerId();
		Seller seller = ss.selcetSeller(sellerId);
		//查询该店铺售卖多少
		int sellerSold = ods.selectSellerSold(sellerId);
		//查询该商品已售多少
		int totalSold = ods.selectTotalSold(goodsId);
		//查询该商品相关用户评论
		List<UserReviewVO> urevo =  us.selectUserReviewVO(goodsId);
		//组合给前端
		SellerGoodsReviewVO<UserReviewVO> sdrVo = new SellerGoodsReviewVO(goods,urevo,seller,totalSold,sellerSold);
		request.setAttribute("sdrVo", sdrVo);
		response.setCharacterEncoding("utf-8");
		System.out.println(sdrVo);
		request.getRequestDispatcher("purchase.jsp").forward(request, response);
	}
}
